{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7df0a440",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import pyreadr\n",
    "from __future__ import division\n",
    "import pylab as pl\n",
    "from matplotlib.backends.backend_pdf import PdfPages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c1f5808b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "brain_signals = pyreadr.read_r(\"filtered_signals.Rdata\")\n",
    "muscle_signals = pyreadr.read_r(\"filtered_muscle_signals.Rdata\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7a20598c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hbtot_filt_pass = list(brain_signals.items())[1][1]\n",
    "df_hboxy_filt_pass = list(brain_signals.items())[3][1]\n",
    "\n",
    "df_hbtot_filt_pass_muscle = list(muscle_signals.items())[1][1]\n",
    "df_hboxy_filt_pass_muscle = list(muscle_signals.items())[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "123e49cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f_plot_psd(df, num):\n",
    "    \n",
    "    data = df[df[\"number\"] == num]\n",
    "    sampling_rate = 1/0.02\n",
    "    \n",
    "    fourier_transform = np.fft.rfft(data[\"sg_filt\"])\n",
    "    abs_fourier_transform = np.abs(fourier_transform)\n",
    "    power_spectrum = np.square(abs_fourier_transform)\n",
    "    frequency = np.linspace(0, sampling_rate/2, len(power_spectrum))\n",
    "    \n",
    "    psd = {\"freq\":frequency, \"power\":power_spectrum}\n",
    "    df_psd = pd.DataFrame(psd)\n",
    "    df_plot = df_psd[df_psd[\"freq\"] < 25]\n",
    "    df_plot = df_plot[df_plot[\"freq\"] > 0.01]\n",
    "    \n",
    "    fig = plt.figure()  \n",
    "    plt.plot(df_plot[\"freq\"], df_plot[\"power\"], color = 'k')\n",
    "    plt.axvline(x = 0.15, color = 'r', linestyle = '--')\n",
    "    plt.xticks(np.arange(0, 25, 1))\n",
    "    \n",
    "    return fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "554076a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            freq         power\n",
      "12      0.010174  3.427629e+07\n",
      "13      0.011021  3.988697e+07\n",
      "14      0.011869  4.645247e+07\n",
      "15      0.012717  2.623391e+07\n",
      "16      0.013565  4.643319e+06\n",
      "...          ...           ...\n",
      "29483  24.995761  1.704893e+06\n",
      "29484  24.996609  7.094201e+05\n",
      "29485  24.997457  7.515377e+05\n",
      "29486  24.998304  1.827832e+06\n",
      "29487  24.999152  1.002799e+05\n",
      "\n",
      "[29476 rows x 2 columns]\n",
      "         number  subject_id Status     Time    THC    sg_filt  high_pass\n",
      "0             1  TM0001CM01     CM  1201.02  85.21  85.260143  48.638968\n",
      "1             1  TM0001CM01     CM  1201.04  81.22  81.019429  44.368160\n",
      "2             1  TM0001CM01     CM  1201.06  85.20  85.500857  48.819496\n",
      "3             1  TM0001CM01     CM  1201.08  86.11  81.422286  44.710831\n",
      "4             1  TM0001CM01     CM  1201.10  69.40  75.178000  38.436451\n",
      "...         ...         ...    ...      ...    ...        ...        ...\n",
      "7176388      95  TM2029HV01     HV  1980.82  60.33  62.594571   0.603425\n",
      "7176389      95  TM2029HV01     HV  1980.84  58.60  59.457143  -2.531942\n",
      "7176390      95  TM2029HV01     HV  1980.86  66.32  67.732571   5.745550\n",
      "7176391      95  TM2029HV01     HV  1980.88  74.22  73.278286  11.293330\n",
      "7176392      95  TM2029HV01     HV  1980.90  56.55  56.785429  -5.197460\n",
      "\n",
      "[7176393 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "data = df_hbtot_filt_pass[df_hbtot_filt_pass[\"number\"] == 1]\n",
    "sampling_rate = 1/0.02\n",
    "    \n",
    "fourier_transform = np.fft.rfft(data[\"sg_filt\"])\n",
    "abs_fourier_transform = np.abs(fourier_transform)\n",
    "power_spectrum = np.square(abs_fourier_transform)\n",
    "frequency = np.linspace(0, sampling_rate/2, len(power_spectrum))\n",
    "    \n",
    "psd = {\"freq\":frequency, \"power\":power_spectrum}\n",
    "df_psd = pd.DataFrame(psd)\n",
    "df_plot = df_psd[df_psd[\"freq\"] < 25]\n",
    "df_plot = df_plot[df_plot[\"freq\"] > 0.01]\n",
    "    \n",
    "print(df_plot)\n",
    "print(df_hbtot_filt_pass)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f193ebe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# TM0003CM01\n",
    "\n",
    "p_cm_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e75ce253",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a247eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58319213",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_cm_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8592cf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TM2003HV01\n",
    "\n",
    "p_hv_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75acd61a",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "763b2a60",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbfe16bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_hv_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a9750c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TM1003UM01\n",
    "\n",
    "p_um_brain_hbtot = f_plot_psd(df_hbtot_filt_pass, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5094800",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_um_muscle_hbtot = f_plot_psd(df_hbtot_filt_pass_muscle, 49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c7dc706",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_um_brain_hboxy = f_plot_psd(df_hboxy_filt_pass, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8875d011",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "p_um_muscle_hboxy = f_plot_psd(df_hboxy_filt_pass_muscle, 49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "0942c75d",
   "metadata": {},
   "outputs": [],
   "source": [
    "pp = PdfPages(\"Outputs/psd_plots.pdf\")\n",
    "\n",
    "pp.savefig(p_cm_brain_hbtot)\n",
    "pp.savefig(p_cm_muscle_hbtot)\n",
    "pp.savefig(p_cm_brain_hboxy)\n",
    "pp.savefig(p_cm_muscle_hboxy)\n",
    "pp.savefig(p_hv_brain_hbtot)\n",
    "pp.savefig(p_hv_muscle_hbtot)\n",
    "pp.savefig(p_hv_brain_hboxy)\n",
    "pp.savefig(p_hv_muscle_hboxy)\n",
    "pp.savefig(p_um_brain_hbtot)\n",
    "pp.savefig(p_um_muscle_hbtot)\n",
    "pp.savefig(p_um_brain_hboxy)\n",
    "pp.savefig(p_um_muscle_hboxy)\n",
    "\n",
    "pp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f09ea6f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
